using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using QuanLyChuyenGia.Controls;
using QuanLyChuyenGia.Forms;
using System.IO;
using System.Threading;
using DevExpress.XtraBars.Ribbon;

namespace QuanLyChuyenGia
{
    public partial class Main : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            frmLogin login = new frmLogin();
            DialogResult dr = login.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                //Load Data
                frmLoad load = new frmLoad();
                load.ShowDialog(this);

                //Load Menu
                LoadMenu();

            }
            else
            {
                this.Close();
            }
        }

        private void LoadMenu()
        {
            int currentUserId = clData._NguoiDung.IdNguoiDung;
            int idNhomQuyen = clData._NguoiDung.IdNhomNguoiDung;
            DataSet ds = new QLBH.tblPhanQuyenUI().GetByIdNhomNguoiDung(idNhomQuyen);

            RibbonPage page = null;
            RibbonPageGroup group = null;
            BarButtonItem item = null;

            if (Utils.DatabaseUtils.TableLib.HasData(ds))
            {

                DataTable dtPhanQuyen = ds.Tables[0];

                DataRow[] pageRows = clData.ChucNang.Tables[0].Select("ChucNangCha is null");

                //Lay ve danh sach page
                foreach (DataRow row in dtPhanQuyen.Rows)
                {
                    foreach (DataRow row2 in pageRows)
                    {
                        if (Convert.ToInt32(row["IdChucNang"]) == Convert.ToInt32(row2["IdChucNang"]))
                        {
                            page = new RibbonPage(row2["TenChucNang"].ToString());
                            this.Ribbon.Pages.Add(page);

                            //Lay ve danh group
                            DataRow[] groupRows = clData.ChucNang.Tables[0].Select("ChucNangCha = " + row["IdChucNang"].ToString());
                            foreach (DataRow groupRow in dtPhanQuyen.Rows)
                            {
                                foreach (DataRow groupRow2 in groupRows)
                                {
                                    if (Convert.ToInt32(groupRow["IdChucNang"]) == Convert.ToInt32(groupRow2["IdChucNang"]))
                                    {
                                        group = new RibbonPageGroup(groupRow2["TenChucNang"].ToString());
                                        page.Groups.Add(group);

                                        //Lay ve danh item
                                        DataRow[] itemRows = clData.ChucNang.Tables[0].Select("ChucNangCha = " + groupRow2["IdChucNang"].ToString());
                                        foreach (DataRow itemRow in dtPhanQuyen.Rows)
                                        {
                                            foreach (DataRow itemRow2 in itemRows)
                                            {
                                                if (Convert.ToInt32(itemRow["IdChucNang"]) == Convert.ToInt32(itemRow2["IdChucNang"]))
                                                {
                                                    item = new BarButtonItem();
                                                    item.Caption = itemRow2["TenChucNang"].ToString();
                                                    item.Id = Convert.ToInt32(itemRow["IdChucNang"].ToString());
                                                    item.LargeImageIndex = Convert.ToInt32(itemRow2["ImagesIndex"]);
                                                    item.Name = "btn_" + itemRow["IdChucNang"].ToString();
                                                    item.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large;
                                                    item.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.Menu_ItemClick);
                                                    group.ItemLinks.Add(item);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void Menu_ItemClick(object sender, ItemClickEventArgs e)
        {

            BarButtonItem item = (BarButtonItem)e.Item;
            int id = item.Id;
            switch (id)
            {
                //Quan ly nhom nguoi dung
                case 10: InitControl(new ucNhomNguoiDung()); return;
                //Quan ly nguoi dung
                case 11: InitControl(new ucNguoiDung()); return;
                //Quan ly nguoi dung
                case 14: new frmChangePass().ShowDialog(); return;
                //Quan ly cua hang
                case 18: InitControl(new ucCuaHang()); return;
                //Quan ly bo phan
                case 19: InitControl(new ucBoPhan()); return;
                //quan ly ca lam viec
                case 22: InitControl(new ucCaLamViec()); return;
                //quan ly doi tac
                case 24: InitControl(new ucDoiTac()); return;
                //quan ly nha cung cap
                case 25: InitControl(new ucNhaCungCap()); return;
                //quan ly khach hang
                case 26: InitControl(new ucKhachHang()); return;
                //quan ly kho hang
                case 28: InitControl(new ucKhoHang()); return;
                //quan ly danh muc
                case 29: InitControl(new ucDanhMuc()); return;
                //quan ly Don vi tinh
                case 30: InitControl(new ucDonViTinh()); return;
                //quan ly san pham
                case 31: InitControl(new ucSanPham()); return;
            }
            
        }
        
        private void btnDSChuyenGia_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitControl(new ucChuyenGia(this));

        }
        
        private void btnCongVie_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitControl(new ucCongViec());
        }

        private void InitControl(Control c)
        {
            this.clientPanel.Controls.Clear();
            c.Dock = DockStyle.Fill;
            this.clientPanel.Controls.Add(c);
        }

        private void btnChuyenMon_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitControl(new ucChuyenMon());
        }

        private void btnLinhVuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitControl(new ucLinhVuc());
        }

        private void btnTieuLinhVuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitControl(new ucTieuLinhVuc());
        }

        private void btnThongTin_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangeProfile obj = new frmChangeProfile();
            obj.ShowDialog();
        }

        private void btnMatKhau_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangePass obj = new frmChangePass();
            obj.ShowDialog();
        }

        private void btnThemMoiChuyenGia_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChuyenGia_Add obj = new frmChuyenGia_Add();
            obj.ShowDialog();
        }

        private static bool result = false;
        private static string fileName = "";

        private void btnExport_ItemClick(object sender, ItemClickEventArgs e)
        {
            Thread t = new Thread(new ThreadStart(ExportData));

            saveFileDialog1.DefaultExt = ".bak";
            saveFileDialog1.Filter = "Export Data |*.bak";
            if (DialogResult.OK == saveFileDialog1.ShowDialog(this))
            {
                fileName = saveFileDialog1.FileName;
                t.Start();

                frmMarquee frm = new frmMarquee();

                while (true)
                {
                    if (t.ThreadState == ThreadState.Stopped)
                    {
                        frm.Close();
                        if (result)
                        {
                            MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Error when export data", "Back", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            
        }

        private void ExportData()
        {
            try
            {
                result = clData.ExportData(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ImportData()
        {
            result = clData.ImportData(fileName);
        }

        private void btnImport_ItemClick(object sender, ItemClickEventArgs e)
        {
            Thread t = new Thread(new ThreadStart(ImportData));

            openFileDialog1.DefaultExt = ".bak";
            openFileDialog1.Filter = "Import Data |*.bak";
            if (DialogResult.OK == openFileDialog1.ShowDialog(this))
            {
                fileName = openFileDialog1.FileName;
                t.Start();

                frmMarquee frm = new frmMarquee();

                while (true)
                {
                    if (t.ThreadState == ThreadState.Stopped)
                    {
                        frm.Close();
                        if (result)
                        {
                            MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            new frmLoad().ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show("Error when export data", "Back", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }

            
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes != MessageBox.Show("Ban co chac chan muon tat chuong trinh?", "Confirm", MessageBoxButtons.YesNo))
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
                if (clData._NguoiDung == null)
                {
                    Login();
                }
            }
        }
    }
}